﻿Imports DTO
Imports System.Data.OleDb


Public Class QuiDinhDAO

    Public Function LayThongSoQuiDinh() As List(Of QuiDinhDTO)
        Dim ds As New List(Of QuiDinhDTO)
        Dim cn As OleDbConnection
        Dim strSQL As String
        'B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
        cn = DataProvider.ConnectDB()
        'B3: Tao chuoi strSQL thao tac CSDL
        strSQL = "Select * From THAMSO"
        'B4: Thuc thi chuoi strSQL
        cn.Open()
        Dim cmd As New OleDbCommand(strSQL, cn)

        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader()
        Dim qd As New QuiDinhDTO()
        While (dr.Read())
            qd = New QuiDinhDTO()
            qd.Ma = dr("STT")
            qd.SLTD = dr("SoLuongToiDa")
            qd.ChiSoPhuThu = dr("ChiSoPhuThu")
            qd.HS = dr("HeSo")

            ds.Add(qd)

        End While
        'B5: Dong ket noi CSDL
        cn.Close()
        Return ds
    End Function

    Public Function CapNhatQuiDinh(ByVal qd As QuiDinhDTO)

        Dim ketqua As Boolean = True
        Dim conn As OleDbConnection = DataProvider.ConnectDB()
        conn.Open()

        Dim lenh As OleDbCommand
        Dim sql As String = "Update THAMSO Set SoLuongToiDa = @sltd,ChiSoPhuThu=@cspt, HeSo= @heso Where STT = @Ma"
        lenh = New OleDbCommand(sql, conn)


        lenh.Parameters.Add("@sltd", OleDbType.Integer)
        lenh.Parameters.Add("@cspt", OleDbType.Single)
        lenh.Parameters.Add("@heso", OleDbType.Single)
        lenh.Parameters.Add("@Ma", OleDbType.Integer)


        lenh.Parameters("@sltd").Value = Integer.Parse(qd.SLTD.ToString())

        lenh.Parameters("@cspt").Value = Single.Parse(qd.ChiSoPhuThu.ToString())
        lenh.Parameters("@heso").Value = Single.Parse(qd.HS.ToString())
        lenh.Parameters("@Ma").Value = Integer.Parse(qd.Ma.ToString())

        lenh.ExecuteNonQuery()
        conn.Close()

        Return ketqua

    End Function
End Class
